﻿@using System.Reflection
@using System.Linq.Expressions

@using Whoever.Framework.Base;
@using Whoever.Patterns.Sample.Business.Core;
@model Whoever.Patterns.Website2.Base.FieldModel
@{
    Type modelType = Model.GetType();
    Type viewModelType = Model.ViewModel.GetType();
    Type entityModelType = Model.ViewModel.EntityType;

    var expParameter = Expression.Parameter(modelType, "m");

    Expression sourceExpression;
    var modelProperty = modelType.GetProperty("ViewModel");
    var expModelProperty = Expression.Property(expParameter, modelProperty);
    var expAsModelType = Expression.TypeAs(expModelProperty, viewModelType);

    //Si existe la propiedad en el viewModel se reemplaza la original de la entity
    if (viewModelType.GetProperty(Model.Property.Name) != null)
    {
        sourceExpression = expAsModelType;
    }
    else
    {
        var entityModelProperty = viewModelType.GetProperty("Entity");
        var expEntityModelProperty = Expression.Property(expAsModelType, entityModelProperty);
        var expAsEntityModelType = Expression.TypeAs(expEntityModelProperty, entityModelType);
        sourceExpression = expAsEntityModelType;
    }

    var prop = Expression.Property(sourceExpression, Model.Property.Name);
}
@{
    //Seria bueno encontrar una mejor solucion a esto. Lo complicado es la definicion de la expression dependiendo del type de la property dinamicamente
    if (Model.Property.PropertyType == typeof(int))
    {
        var expLambda = Expression.Lambda<Func<Whoever.Patterns.Website2.Base.FieldModel, int>>(prop, expParameter);
    <text>
    <div class="control-group">
        <div class="control-label">
            @Html.LabelFor(expLambda)
        </div>
        <div class="controls">
            @Html.TextBoxFor(expLambda, Model.HTMLAttributes)
            @Html.ValidationMessageFor(expLambda)
        </div>
    </div>
    </text>
    }

    if (Model.Property.PropertyType == typeof(int?))
    {
        var expLambda = Expression.Lambda<Func<Whoever.Patterns.Website2.Base.FieldModel, int?>>(prop, expParameter);
    <text>
    <div class="control-group">
        <div class="control-label">
            @Html.LabelFor(expLambda)
        </div>
        <div class="controls">
            @Html.TextBoxFor(expLambda, Model.HTMLAttributes)
            @Html.ValidationMessageFor(expLambda)
        </div>
    </div>
    </text>
    }

    if (Model.Property.PropertyType == typeof(string))
    {
        var expLambda = Expression.Lambda<Func<Whoever.Patterns.Website2.Base.FieldModel, string>>(prop, expParameter);
    <text>
    <div class="control-group">
        <div class="control-label">
            @Html.LabelFor(expLambda)
        </div>
        <div class="controls">
            @Html.TextBoxFor(expLambda, Model.HTMLAttributes)
            @Html.ValidationMessageFor(expLambda)
        </div>
    </div>
    </text>
    }

    if (Model.Property.PropertyType == typeof(decimal))
    {
        var expLambda = Expression.Lambda<Func<Whoever.Patterns.Website2.Base.FieldModel, decimal>>(prop, expParameter);
    <text>
    <div class="control-group">
        <div class="control-label">
            @Html.LabelFor(expLambda)
        </div>
        <div class="controls">
            @Html.TextBoxFor(expLambda, Model.HTMLAttributes)
            @Html.ValidationMessageFor(expLambda)
        </div>
    </div>
    </text>
    }

    if (Model.Property.PropertyType == typeof(decimal?))
    {
        var expLambda = Expression.Lambda<Func<Whoever.Patterns.Website2.Base.FieldModel, decimal?>>(prop, expParameter);
    <text>
    <div class="control-group">
        <div class="control-label">
            @Html.LabelFor(expLambda)
        </div>
        <div class="controls">
            @Html.TextBoxFor(expLambda, Model.HTMLAttributes)
            @Html.ValidationMessageFor(expLambda)
        </div>
    </div>
    </text>
    }

    if (Model.Property.PropertyType == typeof(double))
    {
        var expLambda = Expression.Lambda<Func<Whoever.Patterns.Website2.Base.FieldModel, double>>(prop, expParameter);
    <text>
    <div class="control-group">
        <div class="control-label">
            @Html.LabelFor(expLambda)
        </div>
        <div class="controls">
            @Html.TextBoxFor(expLambda, Model.HTMLAttributes)
            @Html.ValidationMessageFor(expLambda)
        </div>
    </div>
    </text>
    }

    if (Model.Property.PropertyType == typeof(double?))
    {
        var expLambda = Expression.Lambda<Func<Whoever.Patterns.Website2.Base.FieldModel, double?>>(prop, expParameter);
    <text>
    <div class="control-group">
        <div class="control-label">
            @Html.LabelFor(expLambda)
        </div>
        <div class="controls">
            @Html.TextBoxFor(expLambda, Model.HTMLAttributes)
            @Html.ValidationMessageFor(expLambda)
        </div>
    </div>
    </text>
    }

    if (Model.Property.PropertyType == typeof(DateTime))
    {
        var expLambda = Expression.Lambda<Func<Whoever.Patterns.Website2.Base.FieldModel, DateTime>>(prop, expParameter);
    <text>
    <div class="control-group">
        <div class="control-label">
            @Html.LabelFor(expLambda)
        </div>
        <div class="controls">
            @Html.TextBoxFor(expLambda, string.Format("{0}{1}{2}", "{0:", ManagerFactory.AppConfigurationManager.GetConfiguration().DateFormat, "}"), new { @class = "datepicker" })
            @Html.ValidationMessageFor(expLambda)
        </div>
    </div>
    </text>
    }

    if (Model.Property.PropertyType == typeof(DateTime?))
    {
        var expLambda = Expression.Lambda<Func<Whoever.Patterns.Website2.Base.FieldModel, DateTime?>>(prop, expParameter);
    <text>
    <div class="control-group">
        <div class="control-label">
            @Html.LabelFor(expLambda)
        </div>
        <div class="controls">
            @Html.TextBoxFor(expLambda, string.Format("{0}{1}{2}", "{0:", ManagerFactory.AppConfigurationManager.GetConfiguration().DateFormat, "}"), new { @class = "datepicker" })
            @Html.ValidationMessageFor(expLambda)
        </div>
    </div>
    </text>
    }
    if (Model.Property.PropertyType == typeof(bool))
    {
        var expLambda = Expression.Lambda<Func<Whoever.Patterns.Website2.Base.FieldModel, bool>>(prop, expParameter);
    <text>
    <div class="control-group">
        <div class="control-label">
            @Html.LabelFor(expLambda)
        </div>
        <div class="controls">
            @Html.CheckBoxFor(expLambda, Model.HTMLAttributes)
            @Html.ValidationMessageFor(expLambda)
        </div>
    </div>
    </text>
    }   
}
